Page({
  /*
  访客添加
    1：房屋
       1：获取我的房屋
       2:存储渲染
       3：点击某一项要拿到点击项的值，存储渲染
       4：样式高亮处理

  */

  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    startTime: Date.now(),
    houseList: [],
    houseName: '',
    "houseId": "",
    "name": "",
    "gender": 0,
    "mobile": "",
    "visitDate": ""
  },
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  goPassport() {
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index',
    })
  },
  onLoad() {
    this.getHouse()
  },
  // 获取我的房屋
  async getHouse() {
    const res = await wx.http.get('/room')
    this.setData({
      houseList: res.data.map(item => {
        return {
          id: item.id,
          name: item.point + item.building + '号楼' + item.room
        }
      })
    })
    console.log(res);
  },
  // 房屋选择点击
  selectEvent(e) {
    console.log(e);
    this.setData({
      houseName: e.detail.name,
      houseId: e.detail.id
    })
  },
  // 日期确定
  dateConfirm(e) {

    // console.log(wx.utils.formatTime(e.detail));
    this.setData({
      visitDate: wx.utils.formatTime(e.detail),
      dateLayerVisible: false
    })
  },
  // 确定点击事件处理
  async submit() {
    // 验证房屋选择
    if (!this.data.houseId) {
      return wx.utils.toast('请选择房屋')
    }
    // 姓名验证
    if (this.data.name.trim().length < 2) {
      return wx.utils.toast('请正确填写姓名')
    }
    // 手机号
    const reg = /^1[3-9]\d{9}$/
    if (!reg.test(this.data.mobile.trim())) {
      return wx.utils.toast('请正确填写手机号')
    }
    // 日期
    if (!this.data.visitDate) {
      return wx.utils.toast('请选择访问日期')
    }
    // 数据提交
    await wx.http.post('/visitor', {
      "houseId": this.data.houseId,
      "name": this.data.name,
      "gender": this.data.gender,
      "mobile": this.data.mobile,
      "visitDate": this.data.visitDate
    })
    // 提示
    wx.showToast({
      title: '提交访客成功',
    })
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index',
    })

  }

})